package hn.cch.init;

import com.alibaba.fastjson.JSON;
import hn.cch.cache.PlayCache;
import hn.cch.constant.Path;
import hn.cch.model.Play;
import hn.cch.util.FileUtil;
import hn.cch.util.StreamUtil;
import hn.cch.util.StringUtil;
import java.io.File;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class InitPlay {

    private static Logger logger = LoggerFactory.getLogger(InitPlay.class);

    /**
     * 读取JSON格式配置文件初始化玩法信息
     */
    public static void initPlay(File file){
        String playText = StringUtil.fromBytes(FileUtil.toBytes(StreamUtil.toInput(file)));
        //String playText = FileUtil.fromBytes(file);
        List<Play> playList = JSON.parseArray(playText, Play.class);
        logger.info("play size : " + playList.size());
        for (Play play: playList) {
            PlayCache.setKeyValue(play.getPlayId(), play);
            logger.info("play cache : " + play.getPlayId());
        }
        logger.info("play size success");
    }

    public static void main(String[] args) {
        initPlay(new File(Path.PLAY_PATH));

        String playId = "000";
        Play play = (Play) PlayCache.getValue(playId);
        logger.info("playId = " + playId + " -> play = " + play);
    }
}
